﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace CBL
{
    public class CommonBusinessObject
    {
        public const string UniChars =
    "àáảãạâầấẩẫậăằắẳẵặèéẻẽẹêềếểễệđìíỉĩịòóỏõọôồốổỗộơờớởỡợùúủũụưừứửữựỳýỷỹỵÀÁẢÃẠÂẦẤẨẪẬĂẰẮẲẴẶÈÉẺẼẸÊỀẾỂỄỆĐÌÍỈĨỊÒÓỎÕỌÔỒỐỔỖỘƠỜỚỞỠỢÙÚỦŨỤƯỪỨỬỮỰỲÝỶỸỴÂĂĐÔƠƯ";

        public const string KoDauChars =
            "aaaaaaaaaaaaaaaaaeeeeeeeeeeediiiiiooooooooooooooooouuuuuuuuuuuyyyyyAAAAAAAAAAAAAAAAAEEEEEEEEEEEDIIIOOOOOOOOOOOOOOOOOOOUUUUUUUUUUUYYYYYAADOOU";

        public static string UnicodeToKoDau(string s)
        {
            if (string.IsNullOrEmpty(s))
                return s;
            var sb = new StringBuilder();
            foreach (var t in s)
            {
                var pos = UniChars.IndexOf(t);
                sb.Append(pos >= 0 ? KoDauChars[pos] : t);
            }
            return sb.ToString();
        }

        public static string UnicodeToKoDauAndGach(string s)
        {
            if (string.IsNullOrEmpty(s))
                return s;
            s = UnicodeToKoDau(s.Trim());
            return Regex.Replace(s, "[^a-zA-Z0-9]+", "-");
        }

        public static string ToSubString(string p, int length)
        {
            if (string.IsNullOrEmpty(p) || p.Length <= length)
                return p;
            p = p.Substring(0, Math.Min(p.Length, length));
            p = p.Substring(0, p.LastIndexOf(' '));
            return string.Format("{0}...", p);
        }

        public static string EnCode(string s)
        {
            return HttpUtility.HtmlEncode(s.Replace("&nbsp;", " ")).Replace("'", "&#39;");
        }

        public static string EncodeBase64(string toEncode)
        {
            var toEncodeAsBytes = Encoding.UTF8.GetBytes(toEncode);
            var returnValue = Convert.ToBase64String(toEncodeAsBytes);
            return returnValue;
        }

        public static string GetCacheKey(string functionName, IEnumerable<object> listParams, string uniqueText = "")
        {

            return uniqueText.GetHashCode() + "_" + functionName.GetHashCode() + "_" + CalculateInvocationHash(listParams);
        }

        public static long CalculateInvocationHash(IEnumerable<object> listParams)
        {
            return listParams == null ? 0 : listParams.Aggregate(0, (accum, current) =>
                accum * 31 +
                    (
                        current == null ? 0 :
                        (
                            current is Array ? (int)CalculateInvocationHash(((IEnumerable)current).Cast<object>()) : current.GetHashCode()
                        )
                    )
                );
        }

        public static T JsonToObject<T>(string jsonString)
        {
            try
            {
                return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonString);
            }
            catch
            {
                return default(T);
            }
        }

        public static string ObjectToJson(object obj)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(obj);
        }

        public static T ChangeType<T>(object obj)
        {
            if (null == obj)
                return default(T);

            try
            {
                var t = (T)Convert.ChangeType(obj, typeof(T));
                return t;
            }
            catch
            {
                return default(T);
            }
        }

        public static bool ObjectToBoolean(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return false;
            }
            try
            {
                return bool.Parse(value);
            }
            catch
            {
                int i;
                int.TryParse(value, out i);
                return i > 0;
            }
        }
    }
}
